package org.afi.mushroomplayer.components;

import org.afi.mushroomplayer.Constants;
import org.afi.mushroomplayer.MainActivity.OnEventHandleListener;
import org.afi.mushroomplayer.R;
import org.afi.mushroomplayer.model.SongInfo;
import org.afi.mushroomplayer.utils.BaseUtils;

import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class PlayingView extends RelativeLayout implements OnClickListener {

    private ToggleButton mPlayPauseBtn;
    private Button mLikeBtn, mLastBtn, mNextBtn;
    private ProgressBar mProgressBar;
    private TextView mTitle, mArtist, mProTime, mLeftTime;
    private OnCheckedChangeListener mCheckedChangeListener;
    private OnEventHandleListener mEventHandleListener;           

	public PlayingView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

    public void initView(Context context) {
        mPlayPauseBtn = (ToggleButton) findViewById(R.id.play_pause);
        mLastBtn = (Button) findViewById(R.id.last_btn);
        mNextBtn = (Button) findViewById(R.id.next_btn);
        mLikeBtn = (Button) findViewById(R.id.like_btn);
        mTitle = (TextView) findViewById(R.id.title);
        mArtist = (TextView) findViewById(R.id.artist);
        mProTime = (TextView) findViewById(R.id.pro_time);
        mLeftTime = (TextView) findViewById(R.id.left_time);
        mProgressBar = (ProgressBar) findViewById(R.id.time_progress);
        mCheckedChangeListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mEventHandleListener.handleEvent(OnEventHandleListener.EVENT_TYPE_PLAY_MUSIC, null, null, -1);
                } else {
                    mEventHandleListener.handleEvent(OnEventHandleListener.EVENT_TYPE_PAUSE_MUSIC, null, null, -1);
                }
            }
        };
        mLastBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
        mLikeBtn.setOnClickListener(this);
        mPlayPauseBtn.setOnCheckedChangeListener(mCheckedChangeListener);
        mPlayPauseBtn.setChecked(Constants.isPlaying);
    }
    
    public void setSong(SongInfo info) {
        initSong(info);
    }
    
    public void setEventHandleListener(OnEventHandleListener listener) {
        this.mEventHandleListener = listener;
    }

    private void initSong(SongInfo info) {
        mTitle.setText(info.getTitle());    
        mArtist.setText(info.getArtist());
        mLeftTime.setText("-" + BaseUtils.formatTime(info.getDuration()));
        mProgressBar.setMax(info.getDuration());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.next_btn) {
            mEventHandleListener.handleEvent(OnEventHandleListener.EVENT_TYPE_NEXT_SONG, null, null, -1);
        } else if (v.getId() == R.id.last_btn) {
            mEventHandleListener.handleEvent(OnEventHandleListener.EVENT_TYPE_LAST_SONG, null, null, -1);
        } else if (v.getId() == R.id.like_btn) {
            mEventHandleListener.handleEvent(OnEventHandleListener.EVENT_TYPE_LIKE_SONG, null, null, -1);
        }
    }

}
